package Fuente;
import java.io.Reader;
import java_cup.runtime.Symbol;
%%
%class Scanner
%unicode
%cup
%function next_token
%type java_cup.runtime.Symbol
%line
%column
%{
    private TablaSimbolos tabla;
    public Scanner(Reader in, TablaSimbolos t){
    this(in);
    this.tabla = t;
    }
    int lineaActual = 1;
    private static int actualEtq=0;
    private static String nuevaEtq() {
    return "etqL"+(++actualEtq);
    }
%}

%{

    private Symbol symbol(int type){
    return new Symbol(type,yyline,yycolumn);
  }
// Crear un nuevo objeto java_cup.runtime.Symbol con información sobre el token actual con valor
  private Symbol symbol(int type,Object value){
    return new Symbol(type,yyline,yycolumn,value);
  }
%}
%eofval{
    return symbol(sym.EOF);
%eofval}
%eofclose

nuevalinea          = \r|\n|\r\n
decimalLiteral      = 0 | [1-9][0-9]*
InputCharacter      = [^\r\n]
identificador       = [:jletter:] [:jletterdigit:]*
espacio             = [ \t\f]+
//charLiteral         = "'"[:jletter:]"'"
stringLiteral       = "'"{InputCharacter}*"'"
floatLiteral        = [0-9]*"."[0-9]+


%state STRING


%%
//palabras reservadas

"if"        { return symbol(sym.IF);}
"else"      { return symbol(sym.ELSE);}
"while"     { return symbol(sym.WHILE);}
"int"       { return symbol(sym.INT_PALABRA, new String(yytext()));}
"char"      { return symbol(sym.CHAR_PALABRA, new String(yytext()));}
"float"     { return symbol(sym.FLOAT_PALABRA, new String(yytext()));}
"const"     { return symbol(sym.CONST_PALABRA);}
"extern"    { return symbol(sym.EXTERN_PALABRA);}
"void"      { return symbol(sym.VOID_PALABRA, new String(yytext()));}
"static"    { return symbol(sym.STATIC_PALABRA);}
"return"    { return symbol(sym.RETURN_PALABRA);}
"signed"    {return symbol(sym.SIGNED_PALABRA);}
"unsigned"  {return symbol(sym.UNSIGNED_PALABRA);}


{identificador}	{ 
Simbolo s;
if ((s = tabla.buscar(yytext())) == null)
s = tabla.insertar(yytext(),"-");
return symbol(sym.IDENTIFICADOR, s); 
}
{floatLiteral}	{ return symbol(sym.FLOAT_LITERAL, new Float(yytext()));}
{stringLiteral}	{ return symbol(sym.CADENA_LITERAL, new String(yytext()));}

"-"	{ return symbol(sym.RESTA);}
"+"	{ return symbol(sym.SUMA);}
"*"	{ return symbol(sym.MULTIPLICACION);}
"/"	{ return symbol(sym.DIVISION);}

"="	{ return symbol(sym.IGUAL);}

"=="	{ return symbol(sym.IGUAL_IGUAL);}
"!="	{ return symbol(sym.DISTINTO);}
"<"	{ return symbol(sym.MENOR);}
">"	{ return symbol(sym.MAYOR);}
"<="	{ return symbol(sym.MENORIGUAL);}
">="	{ return symbol(sym.MAYORIGUAL);}

"&&"	{ return symbol(sym.LOGICO_Y);}
"||"	{ return symbol(sym.LOGICO_O);}

"("	{ return symbol(sym.PARENTIZQ);}
")"	{ return symbol(sym.PARENTDER);}
"{"	{ return symbol(sym.LLAVEIZQ);}
"}"	{ return symbol(sym.LLAVEDER);}

","	{ return symbol(sym.COMA);}
":"	{ return symbol(sym.DOSPUNTOS);}
";"	{return symbol(sym.SEMI);}
"["	{ return symbol(sym.CORIZQ);}
"]"	{ return symbol(sym.CORDER);}

"?"	{ return symbol(sym.PREGUNTA);}





{nuevalinea} { /* Ignorar nueva linea. */ }
{decimalLiteral} {return symbol(sym.ENTERO,new Integer(yytext())); }
{espacio} { /* Ignorar Espacios en Blanco. */ }
. { System.err.println("Caracter Ilegal: "+yytext()); }






